//logs.js
const util = require('../../utils/util.js')
const md5 = require('../../utils/md5.js')
const http = require('../../utils/http.js');
const publics = require('../../utils/public.js');
Page({
    data: {
        codeMsg: '获取验证码',
        isShow: '1',
        borderColor: [{ value: '', color: '999', select: 1 }, { value: '', color: '999', select: 2 }, { value: '', color: '999', select: 3 }, { value: '', color: '999', select: 4 }],
        colorIndex: '',
        isFocus:false,
        disabledone: false,
        btnClick: '',
        sArr: [],
        isLogin: false,
        codes: ["", "", "", ""],
        inputMsg: [],
        inputValue:'',
        inputpop: false,
        phoneMsg:''
    },
    onLoad: function (val) {
        console.log(val)
        this.setData({
            phoneMsg:val.phone
        })
      wx.setStorageSync('phoneMsg', this.data.phoneMsg)
    },

    inputValue(e) {   
            var text = e.detail.value;
            var textLength = text.length;
            var codeArray = new Array();
            for (var i = 0; i < (textLength > 4 ? 4 : textLength); i++) {
                var code = text.substr(i, 1);
                codeArray[i] = code;
                if (i == 3) {
                    this.setData({
                        isLogin: true,
                        btnClick: 'bgline',
                    })
                } else {
                    this.setData({
                        isLogin: false,
                        btnClick: '',
                    })
                }
            }
            for (var i = codeArray.length; i < 4; i++) {
                codeArray.push("");
            }
            this.setData({
                codes: codeArray
            })
        
    },
    //弹出input
    openKeyboard(){
        this.setData({
            isFocus: true
        })
    },
    setCode() {
        this.setData({
            disabled: false,
            disabledone: true,
            isShow: '0',
        })
        let time = 60;
        let clearTime = setInterval(() => {
            if (time != 0) {
                time--
                this.setData({
                    codeMsg: time + '秒'
                })
            } else {
                clearInterval(clearTime);
                this.setData({
                    codeMsg: '请重新获取验证码'

                })
            }
        }, 1000)
        // 获取短信验证码
        publics.short()
    },
    login() {
        let _this = this
        console.log(_this.data.codes)
        console.log(_this.data.codes.join('') )
        if (this.data.isLogin == true) {
            let data={
                method:'User.binding',
                token:wx.getStorageSync('token'),
                telephone: _this.data.phoneMsg,
                code: _this.data.codes.join('') 
            }
            let arr = http.sorts(data)
            data.sign = md5.hex_md5(arr)
            http._post(data,res=>{
                
                console.log(res)
                if(res.data.code==0){
                    wx.reLaunch({
                        url: '/pages/login/login',
                    })
                }else{
                    wx.showToast({
                        title: res.data.msg,
                        icon:'none',
                        duration:1000
                    })
                }
            })

           
        }
    }

})
